$(function () {
	var id = getQueryString("id");
	if (StringUtils.isEmpty(id)) {
		$("#loginInfo").hide();
	} else {
		vm.getInfo(id);
	}
	$("#edit-form").validate();
});

var vm = new Vue({
	el:'#mapp',
	data:{
		title: "编辑学生信息",
		bsnStudent: {},
		bsnMentor: {}
	},
	methods: {
		saveOrUpdate: function (event) {
			if(!$("#edit-form").valid()) {
				return;
			}
			
			var thisUrl;
			if (StringUtils.isEmpty(vm.bsnStudent.id)) {
				thisUrl = ajaxServerUrlG("bsnstudent/save");
			} else {
				thisUrl = ajaxServerUrlG("bsnstudent/update");
			}
			console.log(thisUrl);
			$.ajax({
				type: "POST",
			    url: thisUrl,
                contentType: "application/json",
			    data: JSON.stringify(vm.bsnStudent),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功');
						if (r.bsnStudent) {
							vm.bsnStudent = r.bsnStudent;
							$("#loginInfo").show();
						}
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(id){
			console.log(StringUtils.isEmpty(id));
			
			if (StringUtils.isEmpty(id)) return;
			var thisUrl = ajaxServerUrlG("bsnstudent/info/" + id);
			$.get(thisUrl, function(r){
					if(r.code === 0){
                		vm.bsnStudent = r.bsnStudent;
                		if (StringUtils.isNotEmpty(vm.bsnStudent.daoshiid)) {
                			vm.getMentor(vm.bsnStudent.daoshiid);
                		}
					}else{
						alert(r.msg);
					} 
            });
		},
		getMentor: function(mentorId) {
			var thisUrl = ajaxServerUrlG("bsnmentor/info/" + mentorId);
			$.get(thisUrl, function(r){
					if(r.code === 0){
                		vm.bsnMentor = r.bsnMentor;
					}else{
						alert(r.msg);
					} 
            });
		},
		gotoLoginInfo: function() {
			if (StringUtils.isEmpty(vm.bsnStudent.id)) {
				alert("请先保存");
				return;
			}
			if (StringUtils.isEmpty(vm.bsnStudent.fuqin)
			    || StringUtils.isEmpty(vm.bsnStudent.shouji1)) {
				alert("创建登录账号前，请先填写父亲姓名和手机号!");
				return;
			}
			var gotoUrl = "msLoginUserEdit.html?loginId=" + vm.bsnStudent.loginId +  
			    "&mobile=" + vm.bsnStudent.shouji1 + 
				"&username=" + vm.bsnStudent.fuqin + 
				"&userType=parent&relationId=" + vm.bsnStudent.id;
			window.location = encodeURI(encodeURI(gotoUrl));
		},
		assignMentor: function() {
			if (StringUtils.isEmpty(vm.bsnStudent.id)) {
				alert("请先保存");
				return;
			}
			var gotoUrl = "msAssignMentor.html?studentId=" + vm.bsnStudent.id;
			window.location = encodeURI(encodeURI(gotoUrl));
		}
	}
});